//
//  RNMpaasWrapper.m
//  RNMpaas
//
//  Created by GJS on 2022/4/15.
//

#import "RNMpaasWrapper.h"

@implementation RNMpaasWrapper

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static RNMpaasWrapper *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
        manager.callbacks = NSMutableDictionary.new;
    });
    return manager;
}

- (NSString *)saveCallback:(NSDictionary *)data context:(PSDContext *)context callback:(PSDJsApiResponseCallbackBlock)callback
{
    NSString *callbackid = data[@"callbackid"] ?: NSUUID.UUID.UUIDString.lowercaseString;
    [self.callbacks setObject:callback forKey:callbackid];
    return callbackid;
}

- (void)callbackTiny:(NSString *)callbackid result:(NSDictionary *)result
{
    PSDJsApiResponseCallbackBlock callback = (PSDJsApiResponseCallbackBlock)(RNMpaasWrapper.shareInstance.callbacks[callbackid]);
    if (callback) {
        //暂时先注释掉，不移除了，可以支持多次回调，实现类似监听事件
        //似乎不支持多次回调，第一次回调能收到，后面的都收不到，所以还是移除了吧，需要实现监听效果，可以再发起tinyToRn调用，附带上callbackid
        [self.callbacks removeObjectForKey:callbackid];
        callback(result);
    }
}

@end
